package com.szqhyxj.youxiangju.biz.card.deposit;

import android.view.View;

import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * XiaoPanYu
 * on 2018/10/31
 */
public class DepositPresenter implements DepositContarct.Presenter {
    private CardModel.Card card;
    private DepositContarct.View view;
    private List<Disposable> disposables = new ArrayList<>();

    public DepositPresenter(DepositContarct.View view) {
        this.view = view;
    }

    @Override
    public void init() {
        disposables.add(CardModel.getDeposit()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new Consumer<BaseResult<List<CardModel.Card>>>() {
                            @Override
                            public void accept(BaseResult<List<CardModel.Card>> listBaseResult) throws Exception {
                                if (listBaseResult.businessLogicSuccess()) {
                                    if(listBaseResult.getData().size()>0){
                                        view.showCardData(View.VISIBLE);
                                        card = listBaseResult.getData().get(0);
                                        setCard(card);
                                    }
                                } else {
                                    view.showToast(listBaseResult.getMessage());
                                }
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                view.showToast(throwable.getMessage());
                            }
                        }));

    }

    @Override
    public void setCard(CardModel.Card card) {
        view.showCardEnd(card.getBankCardNo());
        view.showIcon(card.getCardLogo());
        view.showName(card.getBtName());
    }

    @Override
    public CardModel.Card getCard() {
        return card;
    }

    @Override
    public void onDestroy() {
        for (Disposable disposable : disposables) {
            if (disposable != null) {
                disposable.dispose();
            }
        }
    }
}
